/*****************************************************************************
 * 74hc238.c
 *
 * Copyright (C) 2020 Jeasonvor 1101627719@qq.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 ****************************************************************************/

/*****************************************************************************
 * Included Files
 ****************************************************************************/
#include "plum_api.h"
#include "hardware/decoder/decoder.h"
/*****************************************************************************
 * Trace Definitions
 ****************************************************************************/
#define LOG_RUN_LEVEL LOG_LEVEL_NONE
#define LOG_MODULE    "74hc238"
#include "thirdparty/log/log.h"
/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

/*****************************************************************************
 * Private Types
 ****************************************************************************/

/*****************************************************************************
 * Private Function Prototypes
 ****************************************************************************/

/*****************************************************************************
 * Private Data
 ****************************************************************************/
static dev_decoder_pin_t *decoder_pin;
/*****************************************************************************
 * Public Data
 ****************************************************************************/

/*****************************************************************************
 * Private Functions
 ****************************************************************************/
static dev_decoder_pin_t *dev_decoder_pin(int idx)
{
    return decoder_pin + idx;
}

/*****************************************************************************
 * Public Functions
 ****************************************************************************/

int dev_decoder_init(dev_decoder_pin_t pin[], int cnt)
{
    int rc = 0;

    plum_hal_gpio_cof_t cof = {
        .mode       = PLUM_HAL_GPIO_MODE_COMMON,
        .config.com = PLUM_HAL_GPIO_MODE_OUT_PP,
    };
    plum_u8 loop;
    for (loop = 0; loop < cnt; loop++) {
        plum_hal_gpio_init(pin->pin_a0, &cof);
        plum_hal_gpio_write(pin->pin_a0, PLUM_HAL_GPIO_LOW);

        plum_hal_gpio_init(pin->pin_a1, &cof);
        plum_hal_gpio_write(pin->pin_a1, PLUM_HAL_GPIO_LOW);

        plum_hal_gpio_init(pin->pin_a2, &cof);
        plum_hal_gpio_write(pin->pin_a2, PLUM_HAL_GPIO_LOW);
    }

    decoder_pin = pin;

    return (rc);
}

int dev_decoder_write(int idx, unsigned char code)
{
    int rc = 0;

    LOG_D("idx : %d ,code : %d", idx, code);

    dev_decoder_pin_t *dev = dev_decoder_pin(idx);
    switch (code) {
        case 0x80:
            plum_hal_gpio_write(dev->pin_a0, PLUM_HAL_GPIO_HIGH);
            plum_hal_gpio_write(dev->pin_a1, PLUM_HAL_GPIO_HIGH);
            plum_hal_gpio_write(dev->pin_a2, PLUM_HAL_GPIO_HIGH);
            break;
        case 0x40:
            plum_hal_gpio_write(dev->pin_a0, PLUM_HAL_GPIO_LOW);
            plum_hal_gpio_write(dev->pin_a1, PLUM_HAL_GPIO_HIGH);
            plum_hal_gpio_write(dev->pin_a2, PLUM_HAL_GPIO_HIGH);
            break;
        case 0x20:
            plum_hal_gpio_write(dev->pin_a0, PLUM_HAL_GPIO_HIGH);
            plum_hal_gpio_write(dev->pin_a1, PLUM_HAL_GPIO_LOW);
            plum_hal_gpio_write(dev->pin_a2, PLUM_HAL_GPIO_HIGH);
            break;
        case 0x10:
            plum_hal_gpio_write(dev->pin_a0, PLUM_HAL_GPIO_LOW);
            plum_hal_gpio_write(dev->pin_a1, PLUM_HAL_GPIO_LOW);
            plum_hal_gpio_write(dev->pin_a2, PLUM_HAL_GPIO_HIGH);
            break;
        case 0x08:
            plum_hal_gpio_write(dev->pin_a0, PLUM_HAL_GPIO_HIGH);
            plum_hal_gpio_write(dev->pin_a1, PLUM_HAL_GPIO_HIGH);
            plum_hal_gpio_write(dev->pin_a2, PLUM_HAL_GPIO_LOW);
            break;
        case 0x04:
            plum_hal_gpio_write(dev->pin_a0, PLUM_HAL_GPIO_LOW);
            plum_hal_gpio_write(dev->pin_a1, PLUM_HAL_GPIO_HIGH);
            plum_hal_gpio_write(dev->pin_a2, PLUM_HAL_GPIO_LOW);
            break;
        case 0x02:
            plum_hal_gpio_write(dev->pin_a0, PLUM_HAL_GPIO_HIGH);
            plum_hal_gpio_write(dev->pin_a1, PLUM_HAL_GPIO_LOW);
            plum_hal_gpio_write(dev->pin_a2, PLUM_HAL_GPIO_LOW);
            break;
        case 0x01:
            plum_hal_gpio_write(dev->pin_a0, PLUM_HAL_GPIO_LOW);
            plum_hal_gpio_write(dev->pin_a1, PLUM_HAL_GPIO_LOW);
            plum_hal_gpio_write(dev->pin_a2, PLUM_HAL_GPIO_LOW);
            break;
        default:
            rc = -1;
    }

    return (rc);
}

/****************************************************************************/
/*                                                                          */
/*  End of file.                                                            */
/*                                                                          */
/****************************************************************************/
